﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 银行程序
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            //提前创建了一个账户用于实验。
            Bank bank1 = new Bank();
            ATM atm1 = new ATM();
           /* Account account1 = new Account(111, 111,20000);
            Credit_Account credit_Account1 = new Credit_Account(222, 222);*/
           /* bank1.Add_account(account1);
            bank1.Add_account(credit_Account1);*/
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(bank1, atm1));
        }
    }

    public class Bank
    {
        private int sum_of_account = 0;// 账号总数 
        private int sum_of_credit = 0;//信用卡账户数
        private string name;//银行名称
        public string Name
        {
            get { return name; }
        }
        private Account[] Account_list = new Account[100];
        public Account this[int index]
        {
            set
            {
                Account_list[index] = value;
            }
            get { return Account_list[index]; }
        }
        public Credit_Account[] credit_list = new Credit_Account[100];
        //向银行添加账户
        public void Add_account(Account account)
        {
            if (search_account(account.BankId) == null)
            {
                this[sum_of_account] = account;
                sum_of_account++;
            }

        }

        public void Add_account(Credit_Account credit)
        {
            if (search_account(credit.BankId) == null)
            {
                this[sum_of_account] = credit;
                sum_of_account++;
                credit_list[sum_of_credit] = credit;
                sum_of_credit++;
            }

        }

        //搜索账户
        public Account search_account(long id)
        {
            for (int i = 0; this[i] != null; i++)
            {
                if (this[i].BankId == id)
                {
                    return this[i];
                }
            }
            return null;
        }
        public Credit_Account search_credit_account(long id)
        {
            for (int i = 0; this[i] != null; i++)
            {
                if (credit_list[i].BankId == id)
                {
                    return credit_list[i];
                }
            }
            return null;
        }

    }


    public class Account
    {
        public Account() { }
        public Account(long id, long passWord,long sum)
        {
            this.bank_id = id;
            this.password = passWord;
            this.is_credit = false;
            this.sum = sum;
        }
        public Account(long id,long passWord)
        {
            this.bank_id = id;
            this.password = passWord;
            this.is_credit = false;
        }


        private long bank_id;
        public long BankId
        {
            get { return bank_id; }
            set { bank_id = value; }
        }

        //是否是信用卡
        private bool is_credit;
        public bool IsCredit
        {
            get { return is_credit; }
            set { is_credit = value; }
        }

        //密码
        private long password;
        public long Password
        {
            get { return password; }
            set { password = value; }
        }


       //账户余额
        private long sum=0;
        public long Sum
        {
            get { return sum; }
            set { sum = value; }
        }
        
        // 显示账户余额
        virtual public void Show_sum()
        {
            MessageBox.Show("您的账户余额为："+this.sum);
        }

    }


    public class Credit_Account : Account
    {
        public Credit_Account() { }
        public Credit_Account(long id, long passWord)
        {
            this.BankId = id;
            this.Password = passWord;
            this.IsCredit = true;
        }

       
        // 信用额度与信用余额
        private long line_of_credit = 100000;
        private long rest_of_credit = 100000;

        public long RestOfCredit
        {
            set { rest_of_credit = value; }
            get { return rest_of_credit; }
        }
        public long LineOfCredit
        {
            get { return line_of_credit; }
        }
        public override void Show_sum()
        {
            MessageBox.Show("您的账户信用余额为：" + this.RestOfCredit);
        }

    }

    public class ATM
    {

        public ATM()
        {
            this.BigMoneyFetched += this.Show_warning;
        }

        public delegate void BigMoneyFetchedHandler(Form1 form1 ,BigMoneyArgs bigMoneyArgs);
        public event BigMoneyFetchedHandler BigMoneyFetched;

        public void Show_warning(Form1 form1,BigMoneyArgs bigMoneyArgs)
        {
            MessageBox.Show("此次取款金额巨大，点击再次确认\r\n你的账号是:" + bigMoneyArgs.Bank_Id + "\r\n你此次操作金额为：" + bigMoneyArgs.Big_Sum);
            form1.bigMoneyArgs = bigMoneyArgs;
            form1.check_twice_button.Visible = true;
            form1.cancel_button.Visible=true;
        }


        public void withdraw_money(Form1 form1,Account account, Credit_Account credit, long money, bool is_credit)
        {
            if (money > 10000)
            {
                BigMoneyArgs bigMoneyArgs = new BigMoneyArgs(account.BankId, money);
                BigMoneyFetched(form1,bigMoneyArgs);
            }
            else
            {
                if (is_credit)//是信用卡的话
                {
                    if (money > credit.RestOfCredit)
                    {
                        MessageBox.Show("超出信用额度，取款失败");
                    }
                    else
                    {
                        credit.RestOfCredit = credit.RestOfCredit - money;
                        MessageBox.Show("取款成功，您的账户信用余额为：" + credit.RestOfCredit);
                    }

                }
                else//是非信用卡的话
                {
                    if (money > account.Sum)
                    {
                        MessageBox.Show("超出账户余额，取款失败");
                    }
                    else
                    {
                        account.Sum = account.Sum - money;
                        MessageBox.Show("取款成功，您的账户余额为：" + account.Sum);
                    }

                }
            }
        }

        public void deposite_money(Account account, Credit_Account credit, long money, bool is_credit)
        {
           try
            {
                Bad_Cash();// 检验是否有坏钞
                if (is_credit)//是信用卡的话
                {
                    if (credit.RestOfCredit + money > credit.LineOfCredit)
                    {
                        MessageBox.Show("还款超出信用额度，退还您多余部分");
                        credit.RestOfCredit = credit.LineOfCredit;
                    }
                    else
                    {
                        credit.RestOfCredit = credit.RestOfCredit + money;
                        MessageBox.Show("还款成功，您的账户信用余额为：" + credit.RestOfCredit);

                    }

                }
                else//是非信用卡的话
                {
                    account.Sum = account.Sum + money;
                    MessageBox.Show("存款成功，您的账户余额为：" + account.Sum);
                }
            }
           catch(BadCashException bce)
            {
               MessageBox.Show(bce.Message);
            }
        }     


        //检验坏钞，抛出异常。
        public void Bad_Cash()
        {
            Random random = new Random();
            int a=random.Next(0,10);
            if (a <= 2)//坏钞率定为20%
            {
                throw new BadCashException();
            }
        }

    }


    public class BigMoneyArgs
    {
        BigMoneyArgs() { }
        public BigMoneyArgs(long id, long big_Sum)
        {
            this.Bank_Id = id;
            this.big_sum = big_Sum;
        }
        private long bank_id;
        public long Bank_Id
        {
            get { return bank_id; }
            set { bank_id = value; }
        }
        private long big_sum;
        public long Big_Sum
        {
            get { return big_sum; }
            set { big_sum = value; }
        }
    }

    public class BadCashException:Exception
    {
        public BadCashException(string message )
            : base(message)
        { 

        }
        public BadCashException()
            : base("出现坏钞，行为中止")
        {

        }
    }

}





